package com.fufu.mq.topic;

import com.rabbitmq.client.*;

/**
 * 消费者:主题模式/模糊匹配模式
 */
public class ConsumerTopic {

    public static void main(String[] args) throws Exception {

        String exchangeName = "my-first-topic-exchange";
        String queueName1 = "topic-queue1";
        String queueName2 = "topic-queue2";
        String queueName3 = "topic-queue3";
        String queueName4 = "topic-queue4";

        String routingKey1 = "key1.#";
        String routingKey2 = "key1.key2";
        String routingKey3 = "#.key1";
        String routingKey4 = "key4.*";

        // 创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 服务地址
        factory.setHost("localhost");
        // 端口号
        factory.setPort(5672);
        // 用户名
        factory.setUsername("guest");
        // 密码
        factory.setPassword("guest");

        // 新建连接
        Connection connection = factory.newConnection();
        // 创建信道
        Channel channel = connection.createChannel();

        // 接收消息回调
        DeliverCallback deliverCallback = (s, delivery) -> System.out.println("接收到消息："+ new String(delivery.getBody()) );
        // 中断消息回调
        CancelCallback cancelCallback = (consumerTag) -> System.out.println("消费消息被终端");

        // 消费消息
        channel.basicConsume(queueName3,true, deliverCallback, cancelCallback);
    }
}
